//
//  ViewController.h
//  外星球生活圈
//
//  Created by MR.ZHOU on 15/8/12.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//

#import "ZFHomeViewController.h"
#import "ZFHomeModel.h"
#import "ZFAdsTableViewCell.h"
#import "ZFShopTableViewCell.h"
#import "ZFTableViewCell.h"
#import "ZFLoginViewController.h"
#import "ZFPersonalViewController.h"
#import "ZFGoodtableViewCell.h"

#define DSTableviewHeaderHeight 40
@interface ZFHomeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
   
}

@property(nonatomic,strong) CLLocationManager *manager;
@property(nonatomic,strong) ZFHomeModel *homeModel;
@property(nonatomic,strong) CLLocation *location;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) ZFAdsTableViewCell *header;
@end

@implementation ZFHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftButton.hidden = YES;
  
    _header = [[[NSBundle mainBundle]loadNibNamed:@"ZFAdsTableViewCell" owner:nil options:nil]lastObject];
    
    _header.frame = CGRectMake(0, 0, kUIScreenWidth, 170);
    
    _tableView.tableHeaderView = _header;
    
   
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"生活圈";
    
    [DSLocationManager getLocationSuccess:^(id data) {
        
        _location = data;
         [self getData];
        
    } failed:^(NSError *error) {

        _location = [[CLLocation alloc]initWithLatitude:36.06 longitude:120.38];
         [self getData];

        
    }];
//    
//   [DSLocationManager getLocationSuccess:<#^(CLLocation *locat)success#> failed:^(NSError *error) {
//       <#code#>
//   }]


    
}

-(void)loginMethod{
    if ([ZFUserInfo shareInstance].isLogin) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ZFPersonalViewController *personal= [sb instantiateViewControllerWithIdentifier:@"ZFPersonalViewController"];
        [self.navigationController pushViewController:personal animated:YES];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        
        [app.tab pushHidden:YES];
    }else{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ZFLoginViewController *loginVc= [sb instantiateViewControllerWithIdentifier:@"ZFLoginViewController"];
    [self.navigationController pushViewController:loginVc animated:YES];
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app.tab pushHidden:YES];
    }
}


- (void)getData{
    
    
    //用于存放参数
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:[NSString stringWithFormat:@"%f",_location.coordinate.longitude] forKey:@"lon"];
    
    [param setObject:[NSString stringWithFormat:@"%f",_location.coordinate.latitude] forKey:@"lat"];
  
    
    [param setObject:[DSFunction getTimeScamp] forKey:@"time"];
    
    
    NSArray *array = @[ APP_ID,param[@"lon"],param[@"lat"],param[@"time"],APP_IDENGER];
    [param setObject:[DSFunction md5StringFromArray:array] forKey:@"sign"];
    
    [param setObject:APP_ID forKey:@"app_id"];
    
    //name:itcast
    //passwrod:123
    [DSHTTPClient DSGET:kURLGETHomeInfo withParam:param withSuccessBlock:^(id data) {
    
        DSLog(@"%@",data);
        
       _homeModel  = [[ZFHomeModel alloc]initWithDictionary:data error:nil];
        DSLog(@"%@",_homeModel);
   
        _header.list = _homeModel.focus;
        
        [_tableView reloadData];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    } withErrorBlock:^(id error) {
        
        NSLog(@"请求错误");
    }];
    
}

//- (IBAction)next:(id)sender {
//    
//    //xib加载ViewControll的方法
//    DSShopViewController *shop = [[DSShopViewController alloc]initWithNibName:nil bundle:nil];
//    [self.navigationController pushViewController:shop animated:YES];
//    
//    // 我让tabbar 隐藏 调用一个方法
//    
//    // 1 先拿到我的 UIApplication
//    
//    UIApplication *app = [UIApplication sharedApplication];
//    
//    //2通过Window拿到我的appdelegate
//    AppDelegate *delegate = app.delegate;
//    
//    [delegate.tab pushHidden:YES];
//}

#pragma mark --

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1) {
        
        return 1;
    }
    else if (section == 2){
        return _homeModel.guess.list.count;
    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identifer = @"ZFShopTableViewCell";
        
        ZFShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:identifer bundle:nil]instantiateWithOwner:self options:nil]lastObject];
          
        }
        
        cell.list = _homeModel.group;
        
        return cell;
        
    }else if (indexPath.section == 1){
        static NSString *identifer = @"ZFGoodtableViewCell";
        
        ZFGoodtableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:@"ZFGreatTableCell" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
        }
        
         cell.list = _homeModel.famous;
        return cell;
    }else if (indexPath.section == 2){
        static NSString *identifer = @"ZFTableViewCell";
        
        ZFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:identifer bundle:nil]instantiateWithOwner:self options:nil]lastObject];
        }
        
        cell.model = _homeModel.guess.list[indexPath.row];
        
        return cell;
    }
 
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0 ) {
    }
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kUIScreenWidth , 40)];
    header.backgroundColor = [UIColor yellowColor];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 6)];
    
    line.backgroundColor = [UIColor lightGrayColor];
    
    [header addSubview:line];
    
    
    //添加标题
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 6, 200, 34)];
    
    //titleLabel.backgroundColor = [UIColor clearColor];
    
    if(section == 1){
        
        titleLabel.text = @"名店推荐";
    }else if (section == 2){
        titleLabel.text = @"猜你喜欢";
    }
    
    
    titleLabel.textColor = [UIColor redColor];
    
    //titleLabel.font = [UIFont systemFontOfSize:20];
    
    titleLabel.font = [UIFont fontWithName:@"Menlo" size:20];
    
    titleLabel.textAlignment = NSTextAlignmentLeft;
    
    [header addSubview:titleLabel];
    
    //添加分割线
    
    UIView *sep = [[UIView alloc]initWithFrame:CGRectMake(0, DSTableviewHeaderHeight - 0.5, kUIScreenWidth, 0.5)];
    
    sep.backgroundColor = [UIColor lightGrayColor];
    
    [header addSubview:sep];
    
    return header;
}

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 180;
    }else if(indexPath.section == 1){
        
        return 180;
    }
    return 100;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if (section == 1){
        return DSTableviewHeaderHeight;
    }else if (section == 2){
        return DSTableviewHeaderHeight;
    }else
        return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
